//
//  LoginVC.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/19.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "LoginVC.h"
#import "AKBottomLineTextField.h"
#import "AppDelegate.h"
#import "YBApiManager+Login.h"
#import "NSString+Check.h"

@interface LoginVC ()

@property (weak, nonatomic) IBOutlet AKBottomLineTextField *mobileTF;
@property (weak, nonatomic) IBOutlet AKBottomLineTextField *pwdTF;
@property (weak, nonatomic) IBOutlet UIButton *selectProtocolBtn;


@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self.mobileTF setLeftImageViewWithImageName:@"login_user"];
    [self.pwdTF setLeftImageViewWithImageName:@"login_pwd"];
}

- (IBAction)registerAction:(id)sender {
    [self.navigationController pushViewController:[NSClassFromString(@"RegisterVC") new] animated:YES];
}
- (IBAction)findPwdAction:(id)sender {
    [self.navigationController pushViewController:[NSClassFromString(@"FindPwdVC") new] animated:YES];

}
- (IBAction)loginAction:(id)sender {
    
    NSString *warning = nil;
    if (self.mobileTF.text.isHaveStr == NO) {
        warning = @"请输入正确的手机号/昵称/ID";
    } else if (self.pwdTF.text.isHaveStr == NO) {
        warning = @"请输入密码";
    }
    if (warning) {
        [MBProgressHUD showTipMessageInView:warning];
        return;
    }
    [MBProgressHUD showActivityMessageInView:@""];
    
    [[YBApiManager.shareManager loginWithMobile:self.mobileTF.text password:self.pwdTF.text] subscribeNext:^(id  _Nullable x) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showTipMessageInWindow:@"登录成功" timer:1.0f];
        NSDictionary *dataDic = [x objectForKey:@"data"];
        [UserManager.shareManager saveUserWithInfo:dataDic[@"userinfo"]];
        [UserManager.shareManager updateChatDBPath];
        [UserManager.shareManager updateFriendList];
        [kUsermanager updateGroupChatList];
        [kAppDelegate enterHomeVC];
        
    } error:^(NSError * _Nullable error) {
        [MBProgressHUD hideHUD];
        if ([error isServerError]) {
            [MBProgressHUD showTipMessageInView:error.localizedDescription timer:1.0f];
        } else {
            [MBProgressHUD showTipMessageInView:@"网络错误，请重试" timer:1.0f];
        }
    }];
}
- (IBAction)selectProtocoAction:(UIButton *)sender {
    sender.selected = !sender.selected;
}
- (IBAction)lookProtocolAction:(id)sender {
}


@end
